﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using uComponents.Core.DataTypes.UrlPicker.Dto;
using System.Web;
using System.Web.UI;
using System.IO;
using umbraco.presentation.nodeFactory;

namespace uComponents.RazorModels.UrlPicker
{
    public class RazorUrlPickerState : UrlPickerState
    {
        public HtmlString ToHtml()
        {
            return ToHtml(null);
        }
        public HtmlString ToHtml(string cssClassName)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (TextWriter tw = new StreamWriter(ms))
                {
                    using (HtmlTextWriter writer = new HtmlTextWriter(tw))
                    {
                        Render(writer, cssClassName);
                    }
                    tw.Flush();
                    ms.Position = 0;
                    using (TextReader reader = new StreamReader(ms))
                    {
                        string html = reader.ReadToEnd();
                        return new HtmlString(html);
                    }
                }
            }
        }

        internal void Render(HtmlTextWriter writer, string cssClassName)
        {
            switch (this.Mode)
            {
                case Core.DataTypes.UrlPicker.UrlPickerMode.Content:
                    if (this.NodeId != null)
                    {
                        if (this.NewWindow)
                        {
                            writer.AddAttribute("target", "_blank");
                        }
                        if (!string.IsNullOrEmpty(cssClassName))
                        {
                            writer.AddAttribute(HtmlTextWriterAttribute.Class, cssClassName);
                        }
                        Node node = new Node(this.NodeId.Value);
                        writer.AddAttribute("href", this.Url ?? node.NiceUrl);
                        writer.RenderBeginTag(HtmlTextWriterTag.A);
                        if (!string.IsNullOrEmpty(this.Title))
                        {
                            writer.Write(this.Title.Trim());
                        }
                        else
                        {
                            writer.Write(node.Name);
                        }
                        writer.RenderEndTag();
                    }
                    break;
                case Core.DataTypes.UrlPicker.UrlPickerMode.Media:
                    if (this.NewWindow)
                    {
                        writer.AddAttribute("target", "_blank");
                    }
                    if (!string.IsNullOrEmpty(cssClassName))
                    {
                        writer.AddAttribute(HtmlTextWriterAttribute.Class, cssClassName);
                    }
                    writer.AddAttribute("href", this.Url);
                    writer.RenderBeginTag(HtmlTextWriterTag.A);
                    if (!string.IsNullOrEmpty(this.Title))
                    {
                        writer.Write(this.Title.Trim());
                    }
                    else
                    {
                        writer.Write(" ");
                    }
                    writer.RenderEndTag();
                    break;
                case Core.DataTypes.UrlPicker.UrlPickerMode.Upload:
                    break;
                case Core.DataTypes.UrlPicker.UrlPickerMode.URL:
                    if (this.NewWindow)
                    {
                        writer.AddAttribute("target", "_blank");
                    }
                    if (!string.IsNullOrEmpty(cssClassName))
                    {
                        writer.AddAttribute(HtmlTextWriterAttribute.Class, cssClassName);
                    }
                    writer.AddAttribute("href", this.Url);
                    writer.RenderBeginTag(HtmlTextWriterTag.A);
                    if (!string.IsNullOrEmpty(this.Title))
                    {
                        writer.Write(this.Title.Trim());
                    }
                    else
                    {
                        writer.Write(" ");
                    }
                    writer.RenderEndTag();

                    break;
            }
        }

    }
}
